package com.saas.vertx.web.verticle;

import com.alibaba.fastjson.JSON;
import com.saas.pojo.User;
import com.saas.service.IUserService;
import com.saas.util.AsyncServiceUtil;
import com.saas.vertx.common.ApiHandler;
import com.saas.vertx.common.RouterVerticle;
import io.vertx.core.AbstractVerticle;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * @Author: lzqing
 * @Description: 示例方法
 * @Date: Created in 2021/6/6
 */
@Data
@Component
public class UserVerticle extends AbstractVerticle implements RouterVerticle {

    private IUserService userService = AsyncServiceUtil.getAsyncServiceInstance(IUserService.class);

    @Override
    public void start() throws Exception {
        super.start();
        routers.addAll(CollectionUtils.arrayToList(new ApiHandler[]{
                new ApiHandler(ApiHandler.post(), "/user", res -> {
                    User user = JSON.parseObject(res.getBodyAsString(), User.class);
                    userService.userAdd(user);
                }),
                new ApiHandler(ApiHandler.get(), "/user/:id", res -> {
                    Long id = Long.valueOf(res.pathParam("id"));
                    userService.getUserById(id);
                    res.response().end("u2 ok");
                })}
        ));
    }
}
